#include <iostream>
#include <string>
#include <pthread.h>
#include <unistd.h>

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

void* Routine(void* args)
{
    std::string name = static_cast<char*>(args);
    while (1)
    {
        pthread_mutex_lock(&mutex);
        pthread_cond_wait(&cond, &mutex);
        printf("%s[%ld] is active!\n", name.c_str(), pthread_self());
        pthread_mutex_unlock(&mutex);
    }
}

int main()
{
    pthread_t tid1, tid2, tid3;
    pthread_create(&tid1, nullptr, Routine, (void*)"thread-1");
    pthread_create(&tid2, nullptr, Routine, (void*)"thread-2");
    pthread_create(&tid3, nullptr, Routine, (void*)"thread-3");
    sleep(2);
    std::cout << "main thread begin control..." << std::endl;

    while (1)
    {
        printf("main thread wakeup thread\n");
        // pthread_cond_signal(&cond);
        pthread_cond_broadcast(&cond);
        sleep(1);
    }

    pthread_join(tid1, nullptr);
    pthread_join(tid2, nullptr);
    pthread_join(tid3, nullptr);
    return 0;
}